// package-signin/pages/signin-edit/index.js
const app = getApp();
import { PACKAGE_SIGNIN_PAGE } from '../../../constants/route';
import { ManageTargetInfoUpdateMixins } from '../../../behaviors/page';
import { setSigninGuideFlag, getSigninGuideFlag } from '../../../biztools/wex';
import { navBack, redirectTo, navToAction, showLoading, hideLoading, tipToast } from '../../../common/env';
import { isEmpty, isValidPhone, isValidTel } from '../../../common/utils';
import { ACTIVITY_STATUS_VALUE } from '../../../constants/activity';
import { dealSigninDefaultData } from '../../biztools/signin';
import { getSigninDetail, editSignin, pauseSignin } from '../../api/signin';

Page({
    mixins: [ManageTargetInfoUpdateMixins],
    data: {
        ACTIVITY_STATUS_VALUE,
        signinGuideFlag: false,
        signinInfo: {},
        telModal: {
            show: false,
        },
        inited: false,
    },
    onLoad(options) {
        this._handleGetDetail();
        app.event.addListener('getSigninInfo', this._handleGetSigninInfo.bind(this));
    },
    onUnload() {
        app.event.removeListener('getSigninInfo');
    },
    _handleGetSigninInfo(signinInfo) {
        this.setData({ signinInfo });
    },
    async _handleGetDetail() {
        showLoading('加载中...');
        const { manageTargetInfo } = this.data;
        let signinInfo = await getSigninDetail();
        if (!signinInfo || isEmpty(signinInfo)) {
            signinInfo = dealSigninDefaultData(manageTargetInfo);
        }
        const signinGuideFlag = getSigninGuideFlag() || signinInfo.activityId;
        this.setData({
            signinGuideFlag,
            signinInfo,
        });
        this.setData({ inited: true });
        hideLoading();
    },
    handleOpenFlag() {
        this.setData({
            signinGuideFlag: true,
        });
        setSigninGuideFlag();
    },
    handleShowGuide() {
        this.setData({
            signinGuideFlag: false,
        });
    },
    handleShowTelModal() {
        this.setData({
            'telModal.show': true,
        });
    },
    handleChangeOpen(e) {
        const value = e.detail;
        this.setData({
            'signinInfo.processStatus': value ? ACTIVITY_STATUS_VALUE.processing : ACTIVITY_STATUS_VALUE.paused,
        });
    },
    handleChangeTel(e) {
        this.setData({
            'signinInfo.tel': e.detail,
        });
    },
    handleChangeSigninInfo(e) {
        const signinInfo = e.detail;
        this.setData({ signinInfo });
    },
    handleNavTo(e) {
        const { pageName, ...urlParams } = e.currentTarget ? e.currentTarget.dataset : e;
        const { signinInfo } = this.data;
        navToAction(PACKAGE_SIGNIN_PAGE[pageName], {
            ...urlParams,
        }, {
            success(res) {
                setTimeout(() => {
                    app.event.trigger('sendSigninInfo', signinInfo);
                }, 800);
            },
        });
    },
    async handleSave() {
        let { signinInfo } = this.data;
        console.log('signin-edit data=>', this.data)
        const { activityId, background, tel, activityProductWithSkus, warehouseIds, processStatus } = signinInfo;
        if (processStatus === ACTIVITY_STATUS_VALUE.paused) {
            if (!activityId) {
                navBack();
                return;
            }
            showLoading('保存中...');
            const flag = await pauseSignin({ activityId });
            if (flag) {
                hideLoading();
                navBack();
            }
            return;
        }
        if (!activityProductWithSkus || !activityProductWithSkus.length) {
            tipToast('请添加奖品');
            return;
        }
        if (!background) {
            tipToast('请设置背景封面');
            return;
        }
        if (!warehouseIds.length) {
            tipToast('请选择供货仓');
            return;
        }
        if (!tel || (!isValidPhone(tel) && !isValidTel(tel))) {
            tipToast('请设置正确的联系方式');
            return;
        }
        showLoading('保存中...');
        const info = await editSignin(signinInfo);
        if (info) {
            hideLoading();
            const brandId = this.data.manageTargetId
            redirectTo(PACKAGE_SIGNIN_PAGE['detail'], {
                brandId,
                showSuccess: true,
            });
        }
    },
})
